// index.ts
var menuList = require('../../utils/menu')

Page({
  data: {
    visibleSetting: false,
    starSetting: 0,
    starName: '',
    titleSetting: '',
    countStar: 15,
    menu: [] as Array<menuList>
  },

  onShow() {
    this.queryData();
  },

  queryData() {
    let menu = wx.getStorageSync('menu') || [];
    if (menu.length) {
      let countS = 0;
      menu.forEach((element: { name: string, star: number, starName: string }) => {
        countS += element.star
      });
      this.setData({
        menu: menu,
        countStar: countS
      })
    } else {
      this.setData({
        menu: menuList.menu
      })
    }
  },

  onLoad() {
    this.queryData();
  },

  handleSetting(e: any) {
    let { name, star } = e.currentTarget.dataset;
    this.setData({
      visibleSetting: true,
      titleSetting: name,
      starSetting: star,
      starName: this.starNameSetting(star)
    });
  },

  onStarChange(e: any) {
    const index = e.detail.index;
    this.setData({
      'starSetting': index,
      'starName': this.starNameSetting(index)
    })
  },

  starNameSetting(star: number) {
    let strName = ''
    switch (star) {
      case 0:
        strName = '没见过';
        break;
      case 1:
        strName = '害怕';
        break;
      case 2:
        strName = '吃过';
        break;
      case 3:
        strName = '喜欢';
        break;
      case 4:
        strName = '会做';
        break;
      case 5:
        strName = '天天吃';
        break;
    }
    return strName;
  },

  okSetting() {
    let arr = this.data.menu;
    let count = 0;
    arr.forEach(element => {
      if (element.name == this.data.titleSetting) {
        element.star = this.data.starSetting;
        element.starName = this.data.starName;
        count += this.data.starSetting;
      } else {
        count += element.star;
      }
    });
    this.setData({
      visibleSetting: false,
      menu: arr,
      countStar: count,
    });
    wx.setStorageSync('menu', arr);
  },

  cancelSetting() {
    this.setData({
      visibleSetting: false
    });
  },

  onShareAppMessage: function () {
    return {
      title: '快跟我一起美食制霸!',
      imageUrl: '/images/show.png', //这里是图片的路径
      fail: function (res: any) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }
  },
});
